package com.example.rabbitmq.service;


import com.example.rabbitmq.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class MessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 发送消息
    public void sendMessage(String message) {
        // 生成消息ID
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        System.out.println("发送消息: " + message + "，correlationId: " + correlationData.getId());

        // 发送消息
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.BOOT_EXCHANGE,  // 交换机
                RabbitMQConfig.BOOT_ROUTING_KEY,  // 路由键
                message,  // 消息内容
                correlationData  // 关联数据
        );
    }
}
